import cv2
import time
import os

def capture_photos(save_dir="photos", num_photos=5, camera_index=0, delay=1):
    """
    循环拍摄多张照片
    :param save_dir: 保存图片的文件夹
    :param num_photos: 拍摄数量
    :param camera_index: 摄像头编号
    :param delay: 每次拍照间隔秒数
    """
    # 创建保存目录
    os.makedirs(save_dir, exist_ok=True)

    # 打开摄像头
    cap = cv2.VideoCapture(camera_index)
    if not cap.isOpened():
        print("无法打开摄像头")
        return

    # 等待摄像头启动
    time.sleep(2)

    for i in range(num_photos):
        ret, frame = cap.read()
        if ret:
            filename = os.path.join(save_dir, f"photo_{i+1}.jpg")
            cv2.imwrite(filename, frame)
            print(f"已保存: {filename}")
        else:
            print(f"拍摄第 {i+1} 张失败")
        time.sleep(delay)

    cap.release()
    print("拍摄完成")

if __name__ == "__main__":
    capture_photos(save_dir="/home/pi/dog/foursteps/robocup/capture/pictures", num_photos=5, delay=0.5)
